$(function () {
    //用户统计
    function getUserStatis() {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Statistical/userStatis?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Statistical/userStatis?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                "token": _token
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                // var result = JSON.stringify(data);
                if (data.code == 1) {
                    var DAU = data.data.DAU;
                    var oneDay = data.data.registerNum.oneDay;
                    var sevenDay = data.data.registerNum.sevenDay;
                    var threeDay = data.data.registerNum.threeDay;

                    var chart = new Highcharts.Chart('container', {
                        chart: {
                            type: 'bar'
                        },
                        title: {
                            text: '用户注册量统计人数柱形图'
                        },
                        subtitle: {
                            // text: '数据来源: Wikipedia.org'
                        },
                        xAxis: {
                            categories: ['一天', '三天', '七天'],
                            title: {
                                text: '日期'
                            }
                        },
                        yAxis: {
                            min: 0,
                            title: {
                                text: '注册人数 ',
                                align: 'high'
                            },
                            labels: {
                                overflow: 'justify'
                            }
                        },
                        tooltip: {
                            valueSuffix: ' 个'
                        },
                        plotOptions: {
                            bar: {
                                dataLabels: {
                                    enabled: true
                                    // ,allowOverlap 默认是 false，即不允许数据标签重叠
                                }
                            }
                        },
                        legend: {
                            layout: 'vertical',
                            align: 'right',
                            verticalAlign: 'top',
                            x: -40,
                            y: 100,
                            floating: true,
                            borderWidth: 1,
                            backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
                            shadow: true
                        },
                        credits: {
                            enabled: false
                        },
                        series: [{
                            name: '一天',
                            data: [oneDay]
                        }, {
                            name: '三天',
                            data: [threeDay]
                        }, {
                            name: '七天',
                            data: [sevenDay]
                        }]
                    });
                    $('input').click(function () {
                        var val = $(this).prop('checked'),
                            allowOverlap = val === 'on';
                        console.log(val);
                        chart.update({
                            plotOptions: {
                                bar: {
                                    dataLabels: {
                                        allowOverlap: val
                                    }
                                }
                            }
                        });
                    });
                    //
                }
                else {
                    alert(data.message)
                }
            },
            error: function () {
                alert('查询失败');
            }
        });
    }

    getUserStatis();

    //查询对应列表数据
    $("#UserSearch").click(function () {
        var _searchType = $("#searchType option:selected").val();
        var search_val = $("#search").val();
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/UserManage/UserList?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "GET",
            url: ip + '/Back/UserManage/UserList?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token + '&' + _searchType + '=' + search_val + "&page=0&num=20",
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                // "token": _token
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                // var result = JSON.stringify(data);
                if (data.code == 1) {
                    $("#userTab tbody tr").remove();
                    console.log(data);
                    var count = data.data.length;
                    for (var i = 0; i < count; i++) {
                        var balance = data.data[i].balance || ''; //balance (integer, optional): 余额 ,
                        var buycoin = data.data[i].buycoin || '';  // buycoin (integer, optional): 购买币 ,
                        var givecoin = data.data[i].givecoin || '';   //givecoin (integer, optional): 赠送币 ,
                        var nickname = data.data[i].nickname || '';  //nickname (string, optional): 昵称 ,
                        var phone = data.data[i].phone || ''; //phone (string, optional): 手机号 ,
                        var userid = data.data[i].userid || '';
                        var userstate = data.data[i].userstate || '';  // userstate (integer, optional): 账号转台 ,
                        var tr = "<tr userid=" + userid + " phone=" + phone + "><td>" + userid + "</td><td>" + nickname + "</td><td>" + phone + "</td><td>" + balance + "</td><td>" + givecoin + "</td><td>" + buycoin + "</td><td>" + userstate + "</td><td><button class='historyBtn'><i class='icon-edit'>&nbsp;</i>历史记录</button></td><td><button class='updateMoney' ><i class='icon-trash'>&nbsp;</i>加/减币</button></td></tr>";
                        $("#userTab tbody").append(tr);
                        CoinHistory();
                        $(".table-responsive").fadeIn();
                    }
                }
                else {
                    alert(data.message)
                }
            },
            error: function () {
                alert('查询失败');
            }
        });
    });

    function CoinHistory() {
        //加金币 捡金币
        $(".updateMoney").click(function () {
            $("#userid").val($(this).parent().parent().attr('userid'));
            $(".plus_reduce").fadeIn();
        });
        //历史金币
        $(".historyBtn").click(function () {
            var _timestamp = new Date().getTime();
            var _secret = hex_md5(ip + '/Back/Admin/UpdatePermission?timestamp=' + _timestamp + salt);
            var _phone = parseInt($(this).parent().parent().attr("phone"));
            var _userid = parseInt($(this).parent().parent().attr("userid"));
            $.ajax({
                type: "get",
                url: ip + '/Back/UserManage/MBLog?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token + '&page=1&num=20&phone=' + _phone,// + '&userid=' + _userid,
                dataType: 'json',
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json;charset=utf-8",
                    // 'token': _token
                },
                // data: JSON.stringify({
                //     "name": "string",
                //     "permissionId": 0,
                //     "url": "string"
                //
                // }),
                success: function (data) {
                    // var result = JSON.stringify(data);
                    console.log(data);
                    if (data.code == '1') {
                        $("#historyTable tbody tr").remove();
                        var count = data.data.length;
                        for (var i = 0; i < count; i++) {
                            var addTime = getMyDate(data.data[i].addTime);
                            var payCoin = data.data[i].payCoin;
                            var applepayState = data.data[i].applepayState; //(integer, optional): //ios内购状态 1成功 0失败 2无操作 ,
                            var tr = "<tr><td>" + addTime + "</td><td>" + payCoin + "</td><td>" + applepayState + "</td></tr>"
                            $("#historyTable tbody").append(tr);
                        }
                        $(".gold_history_list").fadeIn();
                    }
                },
                error: function () {
                    alert('查询历史金币失败');
                },
            });
        })
    }

    $(".sure").click(function () {
        $(".gold_history_list").fadeOut();
    });
    $("#closePlusReduce").click(function () {
        $(".plus_reduce").fadeOut();
    });

    //增加 减少金币
    $("#updateCoin").click(function () {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/UserManage/UpdateMB?timestamp=' + _timestamp + salt);
        var _reason = $("#reason").val();
        var _mb = parseInt($("#mb").val());
        var _userid = parseInt($("#userid").val());
        var that = $(this);
        $.ajax({
            type: "post",
            url: ip + '/Back/UserManage/UpdateMB?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                'token': _token
            },
            data: JSON.stringify({
                "mb": _mb,
                "reason": _reason,
                "userid": _userid

            }),
            success: function (data) {
                // var result = JSON.stringify(data);
                console.log(data)
                if (data.code == 1) {
                    alert("data.message");
                    $(".plus_reduce").fadeOut();
                    // that.parent().parent().find("td").eq()
                }
                else {
                    console, log(data.message)
                }
            },
            error: function () {
                alert('修改失败');
            },
        });
    });


})